package day06;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * 要求用户输入一个文件名，并复制当前目录中该文件，并取名为"原文件名_copy.后缀名"
 * 定义两个方法分别使用单字节形式复制，以及字节数组形式复制
 * @author Bonnie
 *
 */
public class Test07 {
	public static void main(String[] args) throws IOException {
		Scanner input = new Scanner(System.in);
		String fname = input.next();
		String newfname = fname.replaceAll("\\.(?=[^\\.]+$)", "_copy.");//正向前瞻匹配 (https://juejin.cn/post/6926819409239474189)
		File fl = new File("./Home_Work/src/day06/"+newfname);
		fl.createNewFile();
//		copy1(fname,newfname);
		copy2(fname,newfname);
		input.close();
	}
	/**
	 * 使用单字节方式复制
	 * @param name 要复制的文件名
	 */
	public static void copy1(String name, String newName) throws IOException {
		FileInputStream fis = new FileInputStream("./Home_Work/src/day06/"+name);
		FileOutputStream fos = new FileOutputStream("./Home_Work/src/day06/"+newName);
		int b;
		while ((b = fis.read()) != -1) {
			fos.write(b);
		}
		fis.close();
		fos.close();
		System.out.println("单字节复制完成");
	}
	/**
	 * 使用字节数组形式复制
	 * @param name 要复制的文件名
	 */
	public static void copy2(String name, String newName) throws IOException {
		FileInputStream fis = new FileInputStream("./Home_Work/src/day06/"+name);
		FileOutputStream fos = new FileOutputStream("./Home_Work/src/day06/"+newName);
		byte[] bs = new byte[1024];
		int len;
		while ((len = fis.read(bs)) != -1) {
			fos.write(bs, 0, len);
		}
		fis.close();
		fos.close();
		System.out.println("字节数组复制完成");
	}
}
